package pro.softzhang.algo.lc0;

import pro.softzhang.algo.common.ListNode;

public class LC2_AddTwoNumbers {
    public static void main(String[] args) {

    }

    static
    class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
            ListNode dummy = new ListNode(0), p = dummy;
            int carry = 0;
            while (l1 != null || l2 != null || carry != 0) {
                int l1v = l1 != null ? l1.val : 0;
                int l2v = l2 != null ? l2.val : 0;
                int sv = l1v + l2v + carry;
                carry = sv / 10;
                ListNode sNode = new ListNode(sv % 10);
                p.next = sNode;
                p = sNode;
                if (l1 != null) l1 = l1.next;
                if (l2 != null) l2 = l2.next;
            }
            return dummy.next;
        }
    }
}
